package org.fxcontainer.actions
{
	import com.developmentarc.core.actions.actions.AbstractAction;
	
	import flash.events.Event;
	
	import org.as3commons.logging.ILogger;
	import org.as3commons.logging.LoggerFactory;
	import org.fxcontainer.commands.ControlInvocationCommand;
	import org.fxcontainer.commands.ProcessNavigateCommand;
	import org.fxcontainer.registry.PageRegistry;
	
	/**
	 * Handles navigation.
	 * @author Izzet_Mustafayev
	 */
	public class ProcessNavigateAction extends AbstractAction
	{
		
		/**
		 * The logger instance. 
		 */
		static private var logger:ILogger = LoggerFactory.getClassLogger(ProcessNavigateAction);
		
		/**
		 * ProcessNavigateAction action constructor registers applyable commands.
		 */
		public function ProcessNavigateAction()
		{
			super();
			addCommand(ProcessNavigateCommand.PAGE_NAVIGATE_CMD);
			addCommand(ProcessNavigateCommand.PAGE_CTRL_INVOKE_CMD);
		}
		
		/**
		 * Performs navigation.
		 * @param command the command to process navigation
		 */
		override public function applyAction(_command:Event):void 
		{
			if (null != _command && _command is ProcessNavigateCommand){
				var command:ProcessNavigateCommand = ProcessNavigateCommand(_command);
				switch(command.type){
					case ProcessNavigateCommand.PAGE_CTRL_INVOKE_CMD:
						logger.debug("Navigate to page \"{0}\" and invokes control \"{1}\".", command.pageName, command.controlName);
						PageRegistry.activate(command.pageName);
						//dispatche the ControlInvocationCommand
						var controlInvocationCommand:ControlInvocationCommand = new ControlInvocationCommand(command.pageName, command.controlName);
						controlInvocationCommand.methodName = command.methodName;
						controlInvocationCommand.methodArgs = command.methodArgs;
						controlInvocationCommand.dispatch();
					break;
					//process default navigation					
					case ProcessNavigateCommand.PAGE_NAVIGATE_CMD:
					default:
						logger.debug("Navigate to page \"{0}\".", command.pageName);
						PageRegistry.activate(command.pageName);
					break;
				}
			}
		}
	}
}